﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Component.Mesh;
using Component.Tools;
using Component.Track;
using Component.UI;
using UnityEngine;

namespace Component.Trains
{
    public class Train
    {
        private TrainNode instance;
        private static Action<TrainNode> Callback { get; set; }
        
        public Train(LineData line, Color lineColor, Vector2 startDir, Action<TrainNode> callback)
        {
            instance = TrainManager.CreateTrain();
            instance.Color = lineColor;
            instance.FollowLine += line;
            instance.RunTask = new Task(instance.MoveTask(startDir));
            Callback = callback;
            Callback(instance);
        }

        public Train(Color color, Vector2 startPos, Quaternion angle)
        {
            instance = TrainManager.CopyTrain(color);
            instance.transform.position = startPos;
            instance.transform.rotation = angle;
            instance.RunTask = new Task(instance.MoveTask(angle, startPos));
            Callback(instance);
        }

        public Train(Vector2 start, Quaternion angle, Color color, int lineId = 0)
        {
            instance = TrainManager.CopyTrain(color);
            if (instance.FollowLine.Points.Count == 0)
            {
                instance.FollowLine += Line.LineData(lineId);
                instance.Color = color;
            }
            instance.transform.position = start;
            instance.transform.rotation = angle;
            instance.RunTask = new Task(instance.MoveTask(angle, start));
            // Callback(instance);
        }


    }

    public class TrainManager
    {
        private static readonly QueuePool<TrainNode> NodePool = new QueuePool<TrainNode>(Resources.Load<GameObject>("Prefabs/Train"), new GameObject("trains").transform);
        private static readonly List<TrainNode> Trains = new List<TrainNode>();
        
        public static TrainNode CopyTrain(Color color)
        {
            TrainNode newTrain = CreateTrain();
            foreach (var train in Trains)
            {
                if (train.Color == color)
                {
                    newTrain.Color = train.Color;
                    newTrain.FollowLine = train.FollowLine;
                }
            }

            return newTrain;
        }
        
        public static TrainNode CreateTrain()
        {
            TrainNode train = NodePool.GetInstance();
            Trains.Add(train);
            return train;
        }

        public static void ResetTrain(TrainNode node)
        {
            Trains.Remove(node);
            NodePool.ResetInstance(node);
        }

        public static void ResetAllTrain()
        {
            for (int i = 0; i < Trains.Count; i++)
            {
                ResetTrain(Trains[i]);
            }
        }

        public static void PauseRunning()
        {
            foreach (var train in Trains)
            {
                train.Pause();
            }
        }

        public static void ContinueRunning()
        {
            foreach (var train in Trains)
            {
                train.Continue();
            }
        }
        
    }

}